package tankgame;

import java.util.Vector;

public class enemyTank extends tank implements Runnable{
    final int MAX_SHOT_NUM = 3;
    public Vector<Shot> Shots = new Vector<>();
    public enemyTank(int x, int y, int direction) {
        super(x, y, direction);
    }

    @Override
    public void run() {
        Shot shot = null;
        while(true){
            if(getIs_alive() && Shots.size() < 5){
                switch(getDirection()){
                    case 0:
                        shot = new Shot(getX() + 20,getY(),0);
                        break;
                    case 1:
                        shot = new Shot(getX() + 60,getY() + 20,1);
                        break;
                    case 2:
                        shot = new Shot(getX() + 20,getY() + 60,2);
                        break;
                    case 3:
                        shot = new Shot(getX() ,getY() + 20,3);
                        break;
                }
            }
            Shots.add(shot);
            new Thread(shot).start();
            switch(getDirection()){
                case 0:
                        for (int i = 0; i < 30; i++) {
                            try {
                                Thread.sleep(50);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                            if(getY() > 0){
                                moveUp();
                            }
                        }

                    break;
                case 1:
                        for (int i = 0; i < 30; i++) {
                            try {
                                Thread.sleep(50);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                            if(getX() + 60 < 1000){
                                moveRight();
                            }
                        }
                    break;
                case 2:
                    for (int i = 0; i < 30; i++) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        if(getY() + 60 < 750){
                            moveDown();
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < 30; i++) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        if(getX() > 0 ){
                            moveLeft();
                        }
                    }
                    break;
            }
            setDirection((int)(Math.random() * 4));;
            if(!getIs_alive()){
                break;
            }
        }

    }
}
